package cn.bytecollege.user.controller;

import cn.bytecollege.common.entity.OrderList;
import cn.bytecollege.common.entity.Page;
import cn.bytecollege.common.entity.User;
import cn.bytecollege.user.service.OrderDetailService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet(name = "OrderDetailListController",value ="/user/orderlist")
public class OrderDetailListController extends HttpServlet {
    OrderDetailService orderDetailService=null;
    public OrderDetailListController(){
        orderDetailService=new OrderDetailService();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session= request.getSession();
        User user=(User) session.getAttribute("user");
        Integer userId=user.getUserId();
        List<OrderList> lists=orderDetailService.orderLists(userId);
        Integer sumProduct=0;
        BigDecimal sumOrders= BigDecimal.valueOf(0);
        String productParticulars=null;
        for(int k=0;k<lists.size();k++){
        sumProduct+=lists.get(k).getProductNumber();
        BigDecimal m=BigDecimal.valueOf(lists.get(k).getProductNumber());
        BigDecimal n=lists.get(k).getProductPrice();
        sumOrders = sumOrders.add(m.multiply(n));
        if(lists.get(k).getProductParticulars().length()>15){
            productParticulars=lists.get(k).getProductParticulars().substring(0,15);
            lists.get(k).setProductParticulars(productParticulars);
        }
        }
        request.setAttribute("orderlist",lists);
        request.setAttribute("sumProduct",sumProduct);
        request.setAttribute("sumOrders",sumOrders);
        request.getRequestDispatcher("/cart.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doPost(request, response);
    }
}
